﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bot.Application.Dto;
using Bot.Application.Services;
using Microsoft.AspNetCore.Mvc;

namespace Bot.WebAPI.Controllers
{
    [Route("api/v3/chatbot")]
    [ApiController]
    public class CategoriesController : ControllerBase
    {

        private readonly ICategoryAppService _app;
        public CategoriesController(ICategoryAppService app)
        {
            this._app = app;
        }

        [HttpGet]
        [Route("categories/{id:Guid}")]
        public ActionResult<CategoryDto> Get([FromRoute] Guid id)
        {
            CategoryDto category = _app.Get(id);
            return Ok(category);
        }

        [HttpGet]
        [Route("bots/{botId:Guid}/categories")]
        public ActionResult<IEnumerable<CategoryDto>> GetList([FromRoute] Guid botId)
        {
            IEnumerable<CategoryDto> list = _app.GetByBotId(botId);
            return Ok(list);
        }

        [HttpPut]
        [Route("categories/{id:Guid}")]
        public ActionResult<CategoryDto> Put([FromRoute] Guid id, [FromBody] CategoryUpdateDto dto)
        {
            CategoryDto category = _app.Update(id, dto);
            return Ok(category);
        }

        [HttpPost]
        [Route("bots/{botId:Guid}/categories")]
        public ActionResult<CategoryDto> Post([FromRoute] Guid botId, [FromBody] CategoryCreateDto dto)
        {
            CategoryDto category = _app.Add(botId,dto);
            return CreatedAtAction(nameof(Get), new { id = category.Id }, category);
        }

        [HttpDelete]
        [Route("categories/{id:Guid}")]
        public ActionResult<CategoryDto> Delete([FromRoute] Guid id)
        {
            _app.Delete(id);
            return NoContent();
        }

        [HttpDelete]
        [Route("categories/{id:Guid}:reassign")]
        public ActionResult<CategoryDto> Reassign([FromRoute] Guid id, [FromQuery] Guid targetCategoryId)
        {
            _app.Reassign(id, targetCategoryId);
            return NoContent();
        }
    }
}
